# 管理员功能接口层

from lib import common
from db import db_handler

logger = common.get_logger('admin')


def change_balance_interface(username, balance):
    """
    修改用户余额接口

    return -> flag, msg
    True  修改成功
    False 用户不存在
    """
    userdata = db_handler.select(username)
    if not userdata:
        return False, '用户[{}]不存在，请重新输入'.format(username)
    pre_balance = userdata.get('balance')
    userdata['balance'] = balance
    db_handler.save(userdata)
    msg = '用户[{}]余额从[{:.2f}]修改为[{:.2f}]🐑'.format(username, pre_balance, balance)
    logger.info(msg)
    return True, msg


def lock_user_interface(username):
    """
    锁定用户接口

    return -> flag, msg
    True  用户锁定成功
    False 用户不存在
    """
    userdata = db_handler.select(username)
    if not userdata:
        return False, '用户[{}]不存在，请重新输入'.format(username)
    userdata['locked'] = True
    db_handler.save(userdata)
    msg = '用户[{}]已被锁定'.format(username)
    logger.info(msg)
    return True, msg
